/**
 * 按钮: edit, save, add, copy, remove, slot
 * 三种状态：
 * edit，显示按钮：save remove
 * view，显示按钮：edit remove slot
 * commit，显示按钮：none
 */
type State = "edit" | "view" | "commit";
type Buttons =
  | "edit"
  | "save"
  | "add"
  | "copy"
  | "remove"
  | "slot"
  | "confirm"
  | "delete";

type StateButtons = {
  [btn in Buttons]: boolean;
};

// eidt
const editState: StateButtons = {
  save: true,
  add: true,
  remove: true,
  slot: true,

  confirm: true,
  delete: true,

  edit: false,
  copy: false,
};

// view
const viewState: StateButtons = {
  edit: true,
  add: true,
  remove: true,
  copy: true,
  slot: true,

  confirm: true,
  delete: true,

  save: false,
};

// commit
const commitState: StateButtons = {
  edit: false,
  remove: false,
  slot: false,
  save: false,
  add: false,
  copy: false,
  confirm: false,
  delete: false,
};

const StateMap = {
  edit: editState,
  view: viewState,
  commit: commitState,
};

export const useTableRowState = (stateMap?: {
  [key in State]?: Partial<StateButtons>;
}) => {
  let StateMap = {
    edit: editState,
    view: viewState,
    commit: commitState,
  };

  if (stateMap) {
    StateMap = Object.assign(StateMap, stateMap);
  }

  const btnIsShow = (btn: Buttons, state: State) => {
    if (btn && state) return StateMap[state][btn];
    return false;
  };

  return {
    btnIsShow,
  };
};
